package com.huifer.wgx.controller;

import com.huifer.wgx.model.ProjectEntity;
import com.huifer.wgx.pojo.CommonResult;
import com.huifer.wgx.service.ProjectService;
import com.huifer.wgx.utlis.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.sql.Timestamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title : ProjectController </p>
 * <p>Description : 项目管理</p>
 *
 * @author huifer
 * @date 2019-03-15
 */
@RestController
@RequestMapping("project")
@Api(tags = "ProjectController", description = "项目管理")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    private CommonResult commonResult = new CommonResult();

    @ApiOperation("项目列表")
    @GetMapping("/list")
    public ResponseEntity list(){
        try {
            return ResponseEntity.ok(commonResult.success(projectService.list()));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }

    }


    @ApiOperation("创建项目")
    @PostMapping("/save")
    public ResponseEntity saveProject(
            @RequestParam("project_name") String projectName,
            @RequestParam("start_time") String startTime,
            @RequestParam("project_scheduled_time") Integer projectScheduledTime,
            @RequestParam("personnel_name") String personnelName
    ) {
        try {
            ProjectEntity projectEntity = projectService
                    .saveProject(projectName, DateUtil.getTimestamp(DateUtil.getDateymd(startTime)),
                            projectScheduledTime,
                            personnelName);
            return ResponseEntity.ok(commonResult.success(projectEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("修改项目(特指项目结束)")
    @PutMapping("/edit")
    public ResponseEntity findAllProject(
            @RequestParam("project_name") String projectName,
            @RequestParam("end_time") String endTime) {
        try {
            Timestamp timestamp = DateUtil.getTimestamp(DateUtil.getDateymd(endTime));
            return ResponseEntity
                    .ok(commonResult.success(projectService.updateProject(projectName,
                            DateUtil.getTimestamp(DateUtil.getDateymd(endTime)))));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("查询所有项目")
    @GetMapping("/find_all")
    public ResponseEntity findAllProject(
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(projectService.findAll(pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

}
